


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Header
#include "Main.h"
#include "SyncHandler.h"

#include <AstdTapi.h>


#define IDM_Account 54790
#define IDM_Profile 54791
#define IDM_About 54792
#define IDM_Exit 54793

class CMainWnd
{
private:
	STATIC HWND m_hMainWnd;
	STATIC HMENU m_hPopupMenu;
	STATIC WNDPROC m_pMainWndProc;
	STATIC LPSPropValue m_pNewMsgEntry;
	STATIC SyncHandler* m_pThis;

};


// Attach to main window to display notification and menu extension
if (m_pMainWndProc == NULL)
{
EnumWindows(EnumWndProc, (LPARAM) this);
}

protected:
// Property sheet
STATIC INT_PTR CALLBACK AccountDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
STATIC INT_PTR CALLBACK ProfileDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

protected:
// Main window
STATIC BOOL CALLBACK EnumWndProc(HWND hWnd, LPARAM lParam);
STATIC LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HRESULT NotifyNewMsg(LPMESSAGE pMsg, PCTSTR ptzContent, PCTSTR ptzSenderName = TEXT(""));

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main window subclass
LRESULT CALLBACK SyncHandler::MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_INITMENUPOPUP:
		if ((lParam == 0x00008284) || // PPC & SP 6.0
			((HMENU) wParam == GetSubMenu((HMENU) SendMessage(SHFindMenuBar(hWnd), SHCMBM_GETMENU, 0, 0), 0)))	// SP 2003/5.0
		{
			MENUITEMINFO mi;
			mi.cbSize = sizeof(MENUITEMINFO);
			mi.fMask = MIIM_DATA;
			if (!GetMenuItemInfo((HMENU) wParam, IDM_Account, FALSE, &mi))
			{
				if (!m_hPopupMenu || !GetMenuItemInfo((HMENU) m_hPopupMenu, IDM_Account, FALSE, &mi))
				{
					m_hPopupMenu = CreatePopupMenu();
					//AppendMenu(m_hPopupMenu, MF_STRING, IDM_Account, CeleCtrl::GetDlgTitle(IDD_Account));
//				AppendMenu(m_hPopupMenu, MF_STRING, IDM_Profile, CeleCtrl::GetDlgTitle(IDD_Profile));
//					AppendMenu(m_hPopupMenu, MF_STRING, IDM_About, CeleCtrl::GetDlgTitle(IDD_About));
#ifdef _DEBUG
					AppendMenu(m_hPopupMenu, MF_SEPARATOR, 0, NULL);
					AppendMenu(m_hPopupMenu, MF_STRING, IDM_Exit, TEXT("Exit"));
#endif
				}
				AppendMenu((HMENU) wParam, MF_POPUP, (UINT) m_hPopupMenu, UStrGet(IDS_AppName));
			}
			EnableMenuItem(m_hPopupMenu, IDM_Profile,(m_pThis->m_hLogin == S_OK) ? MF_ENABLED : MF_GRAYED);
		}
		break;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDM_Account:
			m_pThis->DoProperties(hWnd);
			break;

		case IDM_Profile:
			SHInitExtraControls();
//			DialogBox(g_hInst, MAKEINTRESOURCE(IDD_Profile), hWnd, ProfileDlgProc);
			break;

		case IDM_About:
//			DialogBox(g_hInst, MAKEINTRESOURCE(IDD_About), hWnd, AboutDlgProc);
			break;

#ifdef _DEBUG
		case IDM_Exit:
			SendMessage(hWnd, WM_CLOSE, 0, 0);
			break;
#endif

		case IDM_Reply:
		case IDM_Call:
		case IDM_MarkAsRead:
			// Open entry
			ULONG uType;
			LPMESSAGE pMsg;
			if ((m_pNewMsgEntry == NULL) || FAILED(m_pThis->m_pInbox->OpenEntry(m_pNewMsgEntry->Value.bin.cb, (LPENTRYID) m_pNewMsgEntry->Value.bin.lpb,
				NULL, MAPI_BEST_ACCESS, &uType, (LPUNKNOWN*) &pMsg)))
			{
				break;
			}

			// Mark as read
			if (LOWORD(wParam) == IDM_MarkAsRead)
			{
				SPropValue v[1];
				v[0].ulPropTag = PR_MESSAGE_FLAGS;
				v[0].Value.l = MSGFLAG_READ;
				pMsg->SetProps(_NumOf(v), v, NULL);
				pMsg->Release();
				break;
			}

			// Get phone number
			ULONG nVal;
			HRESULT hResult;
			LPSPropValue pAddr;
			SPropTagArray aTags;
			aTags.cValues = 1;
			aTags.aulPropTag[0] = PR_SENDER_EMAIL_ADDRESS;
			hResult = pMsg->GetProps(&aTags, 0, &nVal, &pAddr);
			pMsg->Release();
			if (FAILED(hResult))
			{
				break;
			}

			// Call sender
			if (LOWORD(wParam) == IDM_Call)
			{
				TCHAR tzPhone[MAX_PATH];
				PTSTR p = tzPhone + WStrToTStr(tzPhone, pAddr->Value.lpszW, MAX_PATH) - 2;
				if (*p == '>')
				{
					for (*p = 0; (p >= tzPhone) && (*p != '<'); p--);
					*p++ = 0;
					tapiRequestMakeCall(p, NULL, tzPhone, NULL);
				}
				else
				{
					tapiRequestMakeCall(tzPhone, NULL, NULL, NULL);
				}
				MAPIFreeBuffer(pAddr);
				break;
			}

		case IDM_View:
			// Generate parameters
			TCHAR tzParam[MAX_PATH];
			UStrPrint(tzParam, TEXT("-service \"%s\""), m_pThis->m_tzProfile);
			if (LOWORD(wParam) == IDM_Reply)
			{
				UStrPrint(UStrEnd(tzParam), TEXT("-to \"%s\""), pAddr->Value.lpszW);
				MAPIFreeBuffer(pAddr);
			}

			// Execute command
			SHELLEXECUTEINFO se;
			_Zero(&se);
			se.cbSize = sizeof(SHELLEXECUTEINFO);
			se.lpFile = TEXT("tmail.exe");
			se.lpParameters = tzParam;
			se.nShow = SW_SHOWNORMAL;
			ShellExecuteEx(&se);
			break;

		case IDM_Delete:
			// Delete entry
			if (m_pNewMsgEntry)
			{
				ENTRYLIST e = {1, (SBinary*) &m_pNewMsgEntry->Value.bin};
				m_pThis->m_pInbox->DeleteMessages(&e, 0, NULL, 0);
			}
			break;

		case IDM_Dismiss:
			// We don't need to call SHNotificationRemove, it's dismissed automatically
			break;
		}
		break;

	case WM_DESTROY:
		if (m_hPopupMenu)
		{
			DestroyMenu(m_hPopupMenu);
			m_hPopupMenu = NULL;
		}
		if (m_pNewMsgEntry)
		{
			MAPIFreeBuffer(m_pNewMsgEntry);
			m_pNewMsgEntry = NULL;
		}
		break;
	}

	return CallWindowProc(m_pMainWndProc, hWnd, uMsg, wParam, lParam);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Enum to subclass Inbox main window
BOOL CALLBACK SyncHandler::EnumWndProc(HWND hWnd, LPARAM lParam)
{
	TCHAR tzClass[MAX_NAME];
	GetClassName(hWnd, tzClass, MAX_NAME);
	if (UStrCmp(tzClass, TEXT("Inbox.MainWnd")) == 0)
	{
		m_hMainWnd = hWnd;
		m_pMainWndProc = (WNDPROC) SetWindowLong(hWnd, GWL_WNDPROC, (LONG) MainWndProc);
		return FALSE;
	}
	return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
